From 95eec6779dd3f9e4494b415c4dc22897b1a7b222 Mon Sep 17 00:00:00 2001 From: Tshepang Lekhonkhobe Date: Fri, 26 Jun 2015 20:47:04 +0200 Subject: [PATCH] use $EMAIL environment variable as fallback --- src/cargo/ops/cargo_new.rs | 3 ++- src/doc/config.md | 2 +- tests/test_cargo_new.rs | 16 ++++++++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/cargo/ops/cargo_new.rs b/src/cargo/ops/cargo_new.rs index 3f032b4fa..c8b2fdca6 100644 --- a/src/cargo/ops/cargo_new.rs +++ b/src/cargo/ops/cargo_new.rs @@ -188,7 +188,8 @@ fn discover_author() -> CargoResult<(String, Option)> { user, please set ${}", username_var))) } }; - let email = git_config.and_then(|g| g.get_string("user.email").ok()); + let email = git_config.and_then(|g| g.get_string("user.email").ok()) + .or_else(|| env::var("EMAIL").ok()); let name = name.trim().to_string(); let email = email.map(|s| s.trim().to_string()); diff --git a/src/doc/config.md b/src/doc/config.md index b3f885c1f..e0678e4a2 100644 --- a/src/doc/config.md +++ b/src/doc/config.md @@ -42,7 +42,7 @@ paths = ["/path/to/override"] [cargo-new] # This is your name/email to place in the `authors` section of a new Cargo.toml # that is generated. If not present, then `git` will be probed, and if that is -# not present then `$USER` will be used (with no email). +# not present then `$USER` and `$EMAIL` will be used. name = "..." email = "..." diff --git a/tests/test_cargo_new.rs b/tests/test_cargo_new.rs index b885a49fc..1a8b5c0fa 100644 --- a/tests/test_cargo_new.rs +++ b/tests/test_cargo_new.rs @@ -169,6 +169,22 @@ test!(finds_author_username { assert!(contents.contains(r#"authors = ["foo"]"#)); }); +test!(finds_author_email { + // Use a temp dir to make sure we don't pick up .cargo/config somewhere in + // the hierarchy + let td = TempDir::new("cargo").unwrap(); + assert_that(cargo_process("new").arg("foo") + .env("USER", "bar") + .env("EMAIL", "baz") + .cwd(td.path().clone()), + execs().with_status(0)); + + let toml = td.path().join("foo/Cargo.toml"); + let mut contents = String::new(); + File::open(&toml).unwrap().read_to_string(&mut contents).unwrap(); + assert!(contents.contains(r#"authors = ["bar "]"#)); +}); + test!(finds_author_git { my_process("git").args(&["config", "--global", "user.name", "bar"]) .exec().unwrap(); -- 2.30.2